package org.jeecg.modules.buse.controller;


import com.zhaodui.fxy.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.io.File;
import java.io.InputStream;
import static com.zhaodui.fxy.Base64Utils.download;


//update--begin--author:zhangjiaqiang date:20171030 for:增加swagger支持
@Api(value = "fxyrest", description = "法信云接口", tags = "fxyrestController")
//update--end--author:zhangjiaqiang date:20171030 for:增加swagger支持
@RestController
@RequestMapping(value = "/fxy")
public class fxyrestController {
    @Value(value = "${jeecg.path.upload}")
    private String uploadpath;
    @Autowired
    RedisUtil redisUtil;

    /**
     * 访问地址：http://localhost:8080/jeecg/rest/user/name/{id}
     *
     * @return dosmrz  {"result":{"idcard":"1234567","isok":false,"realname":"曹二众"},"reason":"成功","error_code":0}
     */
    @RequestMapping(value = "/{appCode}/{fxyname}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    //update--begin--author:zhangjiaqiang date:20171030 for:增加swagger支持
    @ApiOperation(value = "根据fxyname进行对应操作", notes = "根据fxyname进行对应操作", httpMethod = "POST", produces = "application/json")
    //update--end--author:zhangjiaqiang date:20171030 for:增加swagger支持
    public Result<?> fkbyname(@ApiParam(required = true, name = "appCode", value = "appCode") @PathVariable("appCode") String appCode, @ApiParam(required = true, name = "fxyname", value = "fxyname") @PathVariable("fxyname") String fxyname, @RequestBody fxyUser fxyuser) {
        String result = null;
//        FxjOrderRmEntity fxjOrderRmEntity = new FxjOrderRmEntity();
        if(StringUtil.isNotEmpty(fxyuser.getFileUrl())){
            fxyuser.setFileUrl(savelocal(fxyuser.getFileUrl(),fxyuser.getFileName()));
        }
        //新增20190925

        if (fxyname.equals("getCorporateBankList")) {//该接口可以用于获取企业对公账号的银行列表。主要用于企业实名申请。
            try {
                result = fxyUtil.getCorporateBankList(appCode,redisUtil,fxyuser);//
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("doAppletQr")) {//该接口用于获取微信小程序签署二维码，签署用户扫码进入小程序后完成签署流程。仅用于法人账号、企业子账号或个人用户。
               try {
                result = fxyUtil.doAppletQr(appCode,redisUtil,fxyuser);//
               } catch (BaseBusinessException e) {
                   return   Result.error(e.getMessage());
               }
        }
        if (fxyname.equals("doAppletParams")) {//该接口用于获取原生APP跳转小程序参数，签署用户进入小程序后完成签署流程。仅用于法人账号、企业子账号或个人用户。

            try {
                result = fxyUtil.doAppletParams(appCode,redisUtil,fxyuser);//
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }


        if (fxyname.equals("registerMainEnterpriseUser")) {//企业注册
            try {
                result = fxyUtil.registMainEnterprise(appCode,redisUtil,fxyuser);//
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }

        if (fxyname.equals("registerSubUser")) {//企业子用户注册接口
            try {

                result = fxyUtil.registerSubUser(appCode,redisUtil,fxyuser);//
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }


        if (fxyname.equals("registerEnterpriseUser")) {// 该接口用于对企业信息进行注册。包括生成法定代表人子用户
            try {
                result = fxyUtil.registerEnterpriseUser(appCode,redisUtil,fxyuser);//
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("postDeliveryAddress")) {// 个人提交送达地址接口
            try {
                result = fxyUtil.postDeliveryAddress(appCode,redisUtil,fxyuser);//
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }

        //新增20190925






        if (fxyname.equals("registerperson")) {//注册
            try {
                result = fxyUtil.registerperson(appCode,redisUtil,fxyuser);//
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("uploadFile")) {//上传
            try {
                result = fxyUtil.uploadFile(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("doRealnameByOCR2Element")) {//二元素验证
            try {
                result = fxyUtil.doRealnameByOCR2Element(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("doRealnameVerifyBy4Element")) {//四要素申请验证
            try {
                result = fxyUtil.doRealnameVerifyBy4Element(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("doRealnameVerifyByResendCode")) {//四要素申请重发验证
            try {
                result = fxyUtil.doRealnameVerifyByResendCode(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("doRealnameVerifyByCode")) {//四要素验证
            try {
                result = fxyUtil.doRealnameVerifyByCode(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("getlpCodebyUserid")) {//获取唇语码
            try {
                result = fxyUtil.getlpCodebyUserid(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("doRealnameVerifyByLipCode")) {//唇语验证
            try {
                result = fxyUtil.doRealnameVerifyByLipCode(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("supplyMaterials")) {//补充材料
            try {
                result = fxyUtil.supplyMaterials(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("extractWrit")) {// 批次提取接口
            try {
                result = fxyUtil.extractWrit(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("doRealnameByCA")) {// 用户生成 CA 接口
            try {
                result = fxyUtil.doRealnameByCA(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("reSendInvitation")) {//重新发送提取码接口
            try {
                result = fxyUtil.reSendInvitation(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        //   该接口用于生成文书，支持同时生成多份文书
        if (fxyname.equals("addAgreementWithBusiness")) {
            try {
                result = fxyUtil.addAgreementWithBusiness(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        //  甘锦坡-法信:
        //你们生成文书接口应该调这个：addAgreementByProcessNumber  该接口用于生成文书，支持同时生成多份文书
        if (fxyname.equals("addAgreementByProcessNumber")) {
            try {
                result = fxyUtil.addAgreementByProcessNumber(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("writList")) {//批次列表接口
                try {
                    result = fxyUtil.writList(appCode,redisUtil,fxyuser);
                } catch (BaseBusinessException e) {
                    return   Result.error(e.getMessage());
                }
            }
        if (fxyname.equals("bidorderDownLoad")) {//申办订单相关材料下载接口
            try {
                result = fxyUtil.bidorderDownLoad(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("goToUserSign")) {//申办订单相关材料下载接口
            try {
                result = fxyUtil.goToUserSign(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }

        if (fxyname.equals("applyCert")) {//申请出证接口
            try {
                result = fxyUtil.applyCert(appCode,redisUtil,fxyuser);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (fxyname.equals("searchBidOrderStatus")) {//订单受理进度查询
            try {
                result = fxyUtil.searchBidOrderStatus(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("reSendInvitation")) {//重新发送提取码接口
            try {
                result = fxyUtil.reSendInvitation(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("doSign")) {//用户按批次编号进行签署
            try {
                result = fxyUtil.doSign(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("doSignWithVerifyCode")) {//用户签署验证码核验接口
            try {
                result = fxyUtil.doSignWithVerifyCode(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("doSignWithSendCode")) {//用户进行签署时先要发送短信验证码
            try {
                result = fxyUtil.doSignWithSendCode(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("doSignWithVerifyCode")) {//用户签署验证码核验接口
            try {
                result = fxyUtil.doSignWithVerifyCode(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("doEnterpriseRealnameRequest")) {
            //该接口用于提交企业实名相关信息。提交申请后。公证云平台审核信息，然后往企业对公账号打款。
            try {
                result = fxyUtil.doEnterpriseRealnameRequest(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("doEnterpriseRealnameVerification")) {//该接口用于对企业实名相关信息进行核验。配合企业实名认证请求接口使用
            try {
                result = fxyUtil.doEnterpriseRealnameVerification(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("addEvid")) {//该接口用于企业用户上传签章
            try {
                result = fxyUtil.addEvid(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("delEvid")) {//该接口用于用户删除签章
            try {
                result = fxyUtil.addEvid(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("serachEvid")) {//该接口用于企业用户查询现有签章
            try {
                result = fxyUtil.addEvid(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("setEvidDefault")) {//该接口用于设置默认签章
            try {
                result = fxyUtil.setEvidDefault(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }
        if (fxyname.equals("getDefaultEvidImg")) {//该接口用于获取用户的默认签章
            try {
                result = fxyUtil.getDefaultEvidImg(appCode,redisUtil,fxyuser);
            } catch (BaseBusinessException e) {
                return   Result.error(e.getMessage());
            }
        }



        return Result.ok(result);
    }
    //图片保存到本地
    public   String savelocal(String imgFile,String filename)
    {
        String imgpath = "";
        InputStream in = null;
        byte[] data = null;
        try
        {
            if(StringUtil.isNotEmpty(imgFile)){
                if(imgFile.startsWith("http")){//如果是网络图片则直接读取
                    String imgurl = imgFile;
                    String ctxPath= uploadpath;//demo中设置为D://upFiles,实际项目应因事制宜
                    String filest = DateUtils.date2Str(DateUtils.yyyymmddhhmmss);
                    imgpath=ctxPath+File.separator+filest +File.separator+filename;
                    File file = new File(ctxPath+File.separator+filest);
                    if (!file.exists()) {
                        file.mkdirs();// 创建文件根目录
                    }
                    System.out.println("imgurl="+imgurl);
                    download(imgurl,imgpath);
                }else{
                    String ctxPath= uploadpath;//demo中设置为D://upFiles,实际项目应因事制宜
                    imgpath=ctxPath+File.separator+imgFile;
                }
            }
        }
         catch (Exception e) {
            e.printStackTrace();
        }
        return  imgpath;
    }
}
